<template>
  <div class="form">
    <div class="header">
      <slot name="header" />
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="(item, index) in formItems" :key="index">
          <el-col v-bind="colLayout">
            <el-form-item v-if="!item.isHidden" :label="item.label" :style="itemLayout">
              <!-- 输入框 -->
              <template v-if="item.type === 'input' || item.type === 'password'">
                <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  :model-value="formValue[item.field]"
                  @update:modelValue="handleValueChange($event, item.field)"
                ></el-input>
              </template>
              <!-- 选择器 -->
              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  style="width: 100%"
                  :model-value="formValue[item.field]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :label="option.title"
                    :value="option.value"
                  >
                  </el-option>
                </el-select>
              </template>
              <!-- 日期选择器 -->
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  :model-value="formValue[item.field]"
                  @update:modelValue="handleValueChange($event, item.field)"
                ></el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer" />
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { IFormItem } from '../types'

export default defineComponent({
  props: {
    formValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    itemLayout: {
      type: Object,
      default: () => ({ padding: '10px 30px' })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xs: 24, // <768px
        sm: 24, // >=768px
        md: 12, // >=992px
        lg: 8, //>=1200px
        xl: 6 // >=1920px
      })
    }
  },
  emits: ['update:formValue'],
  setup(props, { emit }) {
    // 解构出来 避免单向数据流(双向绑定方案一)
    // const formData = ref({ ...props.formValue })
    // watch(
    //   () => props.formValue,
    //   (newValue) => {
    //     formData.value = { ...newValue }
    //   }
    // )
    // watch(
    //   formData,
    //   (newValue) => {
    //     emit('update:formValue', newValue)
    //   },
    //   {
    //     deep: true
    //   }
    // )

    // 方案二
    const handleValueChange = (value: any, field: string) => {
      emit('update:formValue', { ...props.formValue, [field]: value })
    }

    return {
      // formData
      handleValueChange
    }
  }
})
</script>

<style scoped lang="less">
.form {
  padding-top: 22px;
}
</style>
